import { defineStore } from 'pinia'
import { signIn } from '@/api/login/login'
export const loginStore = defineStore('loginStore', {
    state: () => {
        return {
            // 所有这些属性都将自动推断其数据类型
            language: 'zh',
            login: false,
            user_token: '',
            user_name: 1,
            user_avatar: '',
            articleId: '',
            storeId: '',
            users: [
                {
                    id: 1,
                    name: '张三',
                    age: 18,
                },
                {
                    id: 2,
                    name: '李四',
                    age: 20,
                }
            ]
        }
    },
    // 计算属性
    getters: {
        getUserList: (state) => {
            return state.users
        },
        getUserById: (state) => (id: number) => {
            return state.users.find(user => user.id === id)
        }
    },
    actions: {
        updateName(newName:number) {
            this.user_name = this.user_name + newName;
        },
        sendRequest(){
            signIn({page:1,pageSize:10}).then(res=>{
                console.log(res)
            })
        }
    },
    // 数据持久化 原理是存在了缓存里面
    persist:{
        enabled: true,
        // storage: {
        //     getItem(key:any) {
        //         return uni.getStorageSync(key)
        //     },
        //     setItem(key:any, value:any) {
        //         uni.setStorageSync(key, value)
        //     }
        // },
        // #ifdef H5
        strategies: [
            {
                key: 'loginStore',
                storage: sessionStorage, //可选值 sessionStorage ,localStorage
            },
        ],
		// #endif
    }
})
